//
//  ZYXDownSuccessVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/12.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXDownVC.h"
#import "ZYXEmptyCell.h"
#import "ZYXDownCell.h"
#import "YCDownloadManager.h"
#import "ZYXClassNameModel.h"
#import "PlayerViewController.h"
#import "ZYXLayOutView.h"

#import "ZYXPlayVC.h"
@interface ZYXDownVC ()
@property (nonatomic, strong) NSMutableArray *cacheVideoList;
///是否为空
@property (assign,nonatomic) BOOL isEmpty;
@property (strong ,nonatomic) ZYXLayOutView *alterView;
///选中删除 视频
@property (strong,nonatomic) NSIndexPath *selectPath;
@end

@implementation ZYXDownVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //已下载
        self.view.backgroundColor = [UIColor whiteColor];
   
}
#pragma mark MainUI
-(void)setupViews{
      [self.cacheVideoList removeAllObjects];
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-TabBar_Height-HEIGHT(48));
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXDownCell" bundle:nil] forCellReuseIdentifier:@"ZYXDownCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZYXEmptyCell" bundle:nil] forCellReuseIdentifier:@"ZYXEmptyCell"];
    [self.view addSubview:self.tableView];

    [self setupRefresh];
}
- (void)getCacheVideoList {
    
    [self.cacheVideoList removeAllObjects];
    [self.cacheVideoList addObjectsFromArray:[YCDownloadManager  finishList]];
  
    if (self.cacheVideoList.count>0) {
        self.isEmpty = NO;
        NSLog(@"已下载%@",self.cacheVideoList);
        for (YCDownloadItem *item in self.cacheVideoList) {
            ZYXClassNameDerailModel *model = [ZYXClassNameDerailModel infoWithData:item.extraData];
            NSLog(@"已下载的%@",model.title);

        }
           [self.tableView reloadData];
    }else{
        self.isEmpty = YES;
    }
}
#pragma mark 刷新
- (void)setupRefresh {
    NSLog(@"shuaxin");
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
      
        [self getCacheVideoList];
        [self endRefresh];
    }];
}

- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.isEmpty) {
        return 1;
    }else{
        return self.cacheVideoList.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isEmpty) {
        ZYXEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXEmptyCell"];
        cell.imgView.image = IMG(@"wuyixiazai");
        cell.titleLabel.text = @"暂无已下载视频";
        return cell;
    }else{
        ZYXDownCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXDownCell"];
        YCDownloadItem *item = self.cacheVideoList[indexPath.row];
        cell.item = self.cacheVideoList[indexPath.row];
        item.delegate = cell;
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isEmpty) {
        return SCREEN_HEIGHT-NavigationBar_Height-TabBar_Height;
    }else{
        return HEIGHT(41);
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.isEmpty) {
        YCDownloadItem *item  = self.cacheVideoList[indexPath.row];
        ZYXClassNameDerailModel *model = [ZYXClassNameDerailModel infoWithData:item.extraData];
        NSLog(@"已下载的id=%@",model.uid);
        
//        PlayerViewController *playerVC = [[PlayerViewController alloc] init];
//        playerVC.playerItem = item;
//        [self.navigationController pushViewController:playerVC animated:true];
        ZYXPlayVC *vc = [[ZYXPlayVC alloc]init];
        vc.title = model.title;
        vc.playerItem = item;
        [self.navigationController pushViewController:vc animated:YES];
    }
 
  
    
}


-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAK(weakSelf);
    if (self.isEmpty) {
        return nil;
    }else{
    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        self.selectPath = indexPath;
        weakSelf.alterView.hidden = NO;

        NSLog(@"点击了删除");
        
    }];
    deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"#1cca62"];
    
    // 将设置好的按钮放到数组中返回
    return @[deleteRowAction];
    }
}
-(void)clcikSureBtn{
    self.alterView.hidden = YES;
    YCDownloadItem *item = self.cacheVideoList[self.selectPath.row];
    [YCDownloadManager stopDownloadWithItem:item];
    [self.cacheVideoList removeObjectAtIndex:self.selectPath.row];
    if(self.cacheVideoList.count == 0){
        self.isEmpty = YES;
           [self.tableView reloadData];
    }
    [self.tableView reloadData];
}
-(void)viewWillAppear:(BOOL)animated{
    [self getCacheVideoList];
 
    
}
#pragma mark 懒加载
-(NSMutableArray *)cacheVideoList{
    if (!_cacheVideoList) {
        _cacheVideoList = [NSMutableArray array];
    }
    return _cacheVideoList;
}
-(ZYXLayOutView *)alterView{
    if (!_alterView) {
        _alterView = [[NSBundle mainBundle] loadNibNamed:@"ZYXLayOutView" owner:nil options:nil].lastObject;
        _alterView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _alterView.titleLabel.text = @"删除视频";
        _alterView.contentLabel.text = @"您确定要删除当前视频吗？";
        [_alterView.sureBtn addTarget:self action:@selector(clcikSureBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _alterView;
}
@end
